//
//  EmailReceiver.m
//  xjairOA
//
//  Created by apple on 16-1-13.
//  Copyright (c) 2016年 net. All rights reserved.
//

#import "EmailReceiver.h"
#import "Constants.h"
#import "AppDelegate.h"
#import "FootView.h"
#import "EmailDetail.h"

@implementation EmailReceiver

//NSMutableArray * dataArray;
AppDelegate * appDelegate;
NSString * pageNo;
FootView *footView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"收件箱";
    self.tableView.dataSource= self;
    self.tableView.delegate = self;
    pageNo = @"1";
    NSDictionary * params = @{@"pageNo":pageNo};
    self.dataArray = [[NSMutableArray alloc] init];
    appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.manager
     POST:emailSubjects
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         //noticeTitles = responseObject;
         [self.dataArray addObjectsFromArray:responseObject];
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         [self.tableView reloadData];
         // NSLog(@"viewDidLoad %lu",(unsigned long)self.dataArray.count);
         [self addPullToRefreshFooter];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"获取服务器响应出错！");
     }];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //[self.tableView reloadData];
    //NSLog(@"viewWillAppear %lu",(unsigned long)self.dataArray.count);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell * cell = (UITableViewCell*)sender;
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    EmailDetail * emailDetail = (EmailDetail*)segue.destinationViewController;
    [emailDetail setValue: [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"id"] forKey:@"id"];
    
}

//添加FootView指示器
- (void)addPullToRefreshFooter
{
    //FootView初始化
   
 //   if (footView==nil) { //NSLog(@"footview=%@",footView);
        footView = [[FootView alloc]initWithFrame:CGRectMake(0, self.dataArray.count*TABLE_CELL_HIGHT , self.view.bounds.size.width, 251)];
        [self.tableView addSubview:footView];
  //  }else{//NSLog(@"footview=%@",footView);
//        for (UIView * myfootview in [self.tableView subviews]) {
//            if ([myfootview isKindOfClass:[FootView class]]) {
//                [myfootview removeFromSuperview];
//            }
//        }
 //       footView.frame = CGRectMake(0,TABLE_CELL_HIGHT* dataArray.count, self.view.bounds.size.width, 251);
//        [footView refreshStateNomal];
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
  //  }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TABLE_CELL_HIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"emailTitlesCell" forIndexPath:indexPath];
    cell.layer.cornerRadius=12;
    cell.layer.masksToBounds=YES;
    UILabel * label = (UILabel*)[cell viewWithTag:1];
    label.text = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"subject"];
    return cell;
    
}

//当scroller滑动时调用
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (footView.refreshState == RefreshStateNormal&& scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.frame.size.height + 70) {
        [footView refreshStateRelsease];
    }
}

//当滑动结束时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (footView.refreshState == RefreshStateRelease) {
        //[UIView beginAnimations:nil context:nil];
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 70, 0);
        [footView refreshStateLoading];
        //[UIView commitAnimations];
        [self loadDataing];
    }
}


- (void) loadDataing
{
    int pageNoInt = [pageNo intValue];
    pageNoInt++;
    pageNo = [NSString stringWithFormat:@"%d",pageNoInt];
    NSDictionary * params = @{@"pageNo":pageNo};
    [appDelegate.manager
     GET:emailSubjects
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         [self.dataArray addObjectsFromArray:responseObject];
         [footView refreshStateNomal];
         self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
         footView.frame = CGRectMake(0,TABLE_CELL_HIGHT* self.dataArray.count, self.view.bounds.size.width, 251);
         [self.tableView reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"获取服务器响应出错！");
     }];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

@end
